package zj.it.bhne.gridengineeringsurvey.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import zj.it.bhne.gridengineeringsurvey.BaseActivity;
import zj.it.bhne.gridengineeringsurvey.MyApplication;
import zj.it.bhne.gridengineeringsurvey.R;
import zj.it.bhne.gridengineeringsurvey.entity.greendao.Items;
import zj.it.bhne.gridengineeringsurvey.myutils.MyUtils;

/**
 * 添加新项目的活动
 */
public class AddItemActivity extends BaseActivity implements View.OnClickListener {

    private Context context;
    private EditText add_ed_name;
    private EditText add_ed_note;
    private int one;
    private Spinner add_sp_dengji;
    private Spinner add_sp_leixing;


    /**
     * 新添加项目时，如果输入框中有内容，清空
     */
    @Override
    protected void onResume() {
        super.onResume();
        if (!add_ed_name.getText().toString().isEmpty()) {
            add_ed_name.setText("");
        }
        if (!add_ed_note.getText().toString().isEmpty()) {
            add_ed_note.setText("");
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        context = this;
        Intent intent = getIntent();
        one = intent.getIntExtra("add_id", 0);
        initView();
    }


    /**
     * 加载组建，初始化
     **/
    private void initView() {
        add_ed_name = findViewById(R.id.add_ed_name);
        add_ed_name.setOnClickListener(this);
        add_ed_note = findViewById(R.id.add_ed_note);
        add_ed_note.setOnClickListener(this);
        Button add_btn_save = findViewById(R.id.add_btn_save);
        add_btn_save.setOnClickListener(this);

        ImageButton activity_ib_back = findViewById(R.id.activity_ib_back);
        activity_ib_back.setOnClickListener(this);
        TextView activity_tv_title = findViewById(R.id.activity_tv_title);
        activity_tv_title.setOnClickListener(this);
        TextView activity_tv_save = findViewById(R.id.activity_tv_save);
        activity_tv_save.setOnClickListener(this);
        activity_tv_save.setText(getString(R.string.add_item_baocun));
        activity_tv_title.setText(getString(R.string.title_tianjia));
        TextView add_tv_time = findViewById(R.id.add_tv_time);
        add_sp_dengji = findViewById(R.id.add_sp_dengji);
        add_sp_leixing = findViewById(R.id.add_sp_leixing);
        /*Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH) + 1;
        String time = calendar.get(Calendar.YEAR) + "-"
                + month + "-" + calendar.get(Calendar.DAY_OF_MONTH)+"  "+calendar.get(Calendar.HOUR)+":"+calendar.get(Calendar.MINUTE)+":"+calendar.get(Calendar.SECOND);
        */
        Date d = new Date();
        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String endTime = sdf.format(d);
        add_tv_time.setText(endTime);
    }

    /**
     * 搜索方式的点击事件
     **/
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.add_btn_save:
                initAdd();
                break;
            case R.id.activity_ib_back:
                //startActivity(new Intent(context, BottomActivity.class));
                finish();
                break;
            case R.id.activity_tv_save:
                initAdd();
                break;
        }
    }

    /**
     * 添加Item到数据库并保存
     */
    private void initAdd() {
        String name = add_ed_name.getText().toString().trim();//获取输入的项目名称
        String voltage = add_sp_dengji.getSelectedItem().toString().trim();//获取电压等级
        String type = add_sp_leixing.getSelectedItem().toString().trim();//获取建设类型
        String note = add_ed_note.getText().toString().trim();//获取输入的备注信息

        @SuppressLint("SimpleDateFormat") SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, 0);
        Date monday = c.getTime();
        String preMonday = sdf.format(monday);

        //判断输入信息是否为空
        if (name.isEmpty()) {
            MyUtils.getToast(context, "请输入完整信息");
        } else {
            if (name.length() > 50) {
                MyUtils.getToast(context, "请输入50个以下字符");
            } else {
                Items items = new Items();
                items.setName(name);
                items.setId(null);
                items.setNote(note);
                items.setIsCheck(true);
                items.setTime(preMonday);
                items.setState("本地");
                items.setStop("0");
                items.setVoltage(voltage);
                items.setType(type);
                long num = MyApplication.getInstances().getItemsDao().insert(items);
                if (num > 0) {
                    MyUtils.getToast(context, "创建成功");
                    Intent intent = new Intent(context, BottomActivity.class);
                    intent.putExtra("item_id", one);
                    startActivity(intent);
                } else {
                    MyUtils.getToast(context, "创建失败");
                }
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        finish();
    }
}
